package com.edward.action;


import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.ShallCei;
import com.edward.service.ShallCeiManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ShallCeiAction extends ActionSupport {

	private static final long serialVersionUID = 9175975377259165962L;

	private ShallCeiManager shallCeiManager;
	
	private ShallCei shallCei;
	
	private int limit;
	
	private int start;
	
	private boolean flag = false;

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public ShallCei getShallCei() {
		return shallCei;
	}

	public void setShallCei(ShallCei shallCei) {
		this.shallCei = shallCei;
	}

	public void setShallCeiManager(ShallCeiManager shallCeiManager) {
		this.shallCeiManager = shallCeiManager;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}


	@Secured("ROLE_COREFINANCE_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		shallCei.setBalance(shallCei.getMoney() - shallCei.getRepay());
		shallCeiManager.addShallCei(shallCei);
		 response.getWriter().print("{success:true,msg:'添加成功'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String update() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		shallCei.setBalance(shallCei.getMoney() - shallCei.getRepay());
		shallCeiManager.updateShallCei(shallCei);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String delete() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		shallCeiManager.deleteShallCei(shallCei.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		if(flag){
			pager = shallCeiManager.pagerList(start, limit,shallCei);
		}else{
			pager = shallCeiManager.pagerList(start, limit,shallCei.getType());
		}
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	@Secured("ROLE_COREFINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
}
